<?php


namespace App\Http\Controllers\Admin\Product;


use App\Http\Controllers\Controller;
use App\Models\ProductRule;
use App\Traits\Controllers\IndexTraits;
use App\Traits\Controllers\ShowTraits;
use App\Traits\Controllers\StoreTraits;
use App\Traits\Controllers\UpdateTraits;
use App\Transformers\Product\ProductRuleTransformer;
use Illuminate\Http\Request;


class ProductRuleController extends Controller
{

    use IndexTraits;
    use StoreTraits;
    use UpdateTraits;
    use ShowTraits;

    public $model = ProductRule::class;

    public $transformer = ProductRuleTransformer::class;

    /**
     * 删除，支持批量删除
     */
    public function destroy(Request $request)
    {
        //验证用户凭证信息
        $this->validateData($request);

        //得到传过来的id并分割为数组并过滤掉空值
        $ids = array_filter(explode(',',$request->input('ids')));

        if (empty($ids)) {
            return $this->response->errorBadRequest();
        }

        $this->getQuery()->whereIn('id',$ids)->delete();

        return $this->response->noContent();
    }

    /**
     * 参数验证规则
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
            case 'PATCH':
            case 'PUT':
                return [
                    'title' => 'bail|required',
                    'content' => 'bail|required|array'
                ];
                break;
            case 'DELETE':
                return [
                    'ids' => 'bail|required'
                ];
                break;
        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [];
    }

}
